package com.imufe.chemicalenterprisedangerwarning.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imufe.chemicalenterprisedangerwarning.entity.query.CreateGroup;
import com.imufe.chemicalenterprisedangerwarning.entity.query.EnterpriseQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.query.UpdateGroup;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.EnterpriseDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.EnterpriseVO;
import com.imufe.chemicalenterprisedangerwarning.service.EnterpriseService;
import com.imufe.chemicalenterprisedangerwarning.common.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 企业 前端控制器
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-24
 */
@Api(tags = "企业相关")
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    EnterpriseService service;

    @ApiOperation(value = "企业列表", notes = "企业的列表(分页)")
    @GetMapping("/page")
    public ResponseEntity<IPage<EnterpriseVO>> page(@RequestParam(value = "current", defaultValue = "1")Long current) {
        IPage<EnterpriseVO> page = service.page(current);
        return ResponseEntity.success(page);
    }

    @ApiOperation(value = "企业详情", notes = "企业的详细信息")
    @GetMapping("/detailed")
    public ResponseEntity<EnterpriseDetailedVO> detailed(Long id) {
        EnterpriseDetailedVO detailed = service.detailed(id);
        return ResponseEntity.success(detailed);
    }

    @ApiOperation(value = "新增", notes = "新增一个企业")
    @PostMapping("/create")
    public ResponseEntity<EnterpriseDetailedVO> create(@RequestBody @Validated(CreateGroup.class) EnterpriseQuery query) {
        EnterpriseDetailedVO detailedVO = service.create(query);
        return ResponseEntity.success(detailedVO);
    }

    @ApiOperation(value = "删除", notes = "删除一个企业")
    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody Long id) {
        service.removeById(id);
        return ResponseEntity.success();
    }

    @ApiOperation(value = "修改", notes = "修改一个安监局")
    @PutMapping("/update")
    public ResponseEntity<EnterpriseDetailedVO> update(@RequestBody @Validated(UpdateGroup.class) EnterpriseQuery query) {
        EnterpriseDetailedVO detailedVO = service.update(query);
        return ResponseEntity.success(detailedVO);
    }
}